<?php	
    require_once('paths.inc');
    require_once('security.inc');

    require_once('page.inc');    
    require_once('utils.inc');
    require_once(DB_PATH.'db.inc');  
    require(INCLUDE_PATH.'constants.inc');  
    
	session_start();
    checkIsLoggedIn();
	ob_start();
	
	$db = db_connect();
	$id_comanda = $_GET['id'];

	select_language();

	$id_client = $_SESSION['user_logat']->id_client;
	$moneda = $_SESSION['print_factura_moneda'];
    $curs = $_SESSION['print_factura_curs'];
    $curs_invers = $_SESSION['print_factura_curs_invers'];
    $text_factura = $_SESSION['print_factura_text_factura'];
    $serie_factura = $_SESSION['print_factura_serie_factura'];
    $tva = $_SESSION['print_factura_tva'];
    $data = $_SESSION['print_factura_data'];
    $tva_inclus = $_SESSION['print_factura_tva_inclus'];
    $nume_delegat = $_SESSION['print_factura_nume_delegat'];
    $mijloc_transport = $_SESSION['print_factura_mijloc_transport'];
    $cnp = $_SESSION['print_factura_cnp'];
    $serie_ci = $_SESSION['print_factura_serie_ci'];
	
	try {
		$comanda = Comanda::queryLoadById($db, $id_comanda, $id_client);
		if ($comanda->nr_factura_sol == '' && $serie_factura != '') {
			$comanda->nr_factura_sol = $serie_factura;
			$comanda->queryUpdateNrFacturaSol($db);
		}
			
		$firma = Firma::queryLoadById($db, $comanda->id_firma_sol, $id_client);
	}
	catch (QueryLoadComandaException $e) {
		redirectOnError(CT_PRINT_FACTURA_FAIL);
	}
	catch (QueryLoadFirmaException $e) {
		redirectOnError(CT_PRINT_FACTURA_FAIL);
	}
	catch (QueryUpdateComandaException $e) {
		redirectOnError(CT_PRINT_FACTURA_FAIL_UPDATE_COMANDA);
	}
	catch (QueryWrongVersionException $e) {
		redirectOnError(CT_PRINT_FACTURA_FAIL_UPDATE_COMANDA);
	}
        
	header("Pragma: public"); // required                                                                                                                        
    header("Expires: 0");                                                                                                                                    
	header("Content-Description: File Transfer");                                                                                                        
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");                                                                             
	header("Content-type: application/pdf");                                                                                                                 
	header('Content-Transfer-Encoding: binary');                       
	$cale_fisier = USERS_PATH . $_SESSION['user_logat']->id_user . "/factura_" . $id_comanda . ".pdf";                                                                                      
	header("Content-Disposition: attachment; filename=\"" . basename($cale_fisier) . '"');                  

    print_facturaWithOO($db, $comanda, $firma, $moneda, $curs, $curs_invers, $text_factura, $serie_factura, $tva_inclus, $tva, $nume_delegat
                    , $mijloc_transport, $cnp, $serie_ci, $data);
	
	readfile($cale_fisier);                                             
	exit();     
    
?>